function [optionTree, deltahedge, early,earlybetter] = InitialOptionTree(priceZero, ...
   volatility, N, strikePrice, interestRate, type, style)
% This function calculates the price of an option. It uses the function
% stockTree. The OptionTree can have the types: call and put. The styles
% can be European or American.
% Arguments:
%     priceZero:     the initial stock price
%     volatility:    volatility of the stock
%     N:             number of steps to use in the tree
%     strikePrice:   the strike of the options
%     interestRate:  the interest rate used
%     type:          the type of option, either 'put' or 'call'
%     style:         the style of the option, either 'european' or 'american'


% Compute the up and down values and the risk neutral probability of an up
% movement correct the volatility for the number of steps used to approximate
% the value of the option. 
up      = exp(volatility * sqrt(1 / N));
down    = exp(-volatility * sqrt(1 / N));
p       = (exp(interestRate*(1/N)) - down) / (up - down);

% Generate a tree consisting of all the stock prices.
stockTree   = InitStockTree(priceZero, volatility, N);


% Initialize the result matrix.
optionTree  = zeros(N, N);
early = zeros(N, N);
earlybetter= zeros(N,N);

% Instead of using a loop to change all the values in the last column of the
% matrix a vector operation is used. Initialize the last column.
if strcmp('call', type)
	% The value of a call option at experiation is (S - K)^+.
	optionTree(1 : N, N) = max(0, stockTree(1 : N, N) - strikePrice);
elseif strcmp('put', type)
	% The value of a put option at experiation is (K - S)^+.
	optionTree(1 : N, N) = max(0, strikePrice - stockTree(1 : N, N));
else
	% No other types are implemented.
	disp(strcat(type, ' is not implemented yet'));
end

% Use backward recursion to compute the remaining nodes of the tree. This tree 
% can be used for european as well as for american style options.
if strcmp('european', style)
	for j = (N - 1) : -1 : 1
		for i = 1 : j
			optionTree(i, j) = (p * optionTree(i, j + 1) + (1 - p) * ...
				optionTree(i + 1, j + 1)) * exp(-interestRate * 1 / N);
		end
	end
end


if strcmp('american', style)
    % At each node take the greater value of the value derived with the
    % formula used for the European option and the payoff from early
    % exercise. Then use backwards recursion to derive the value from the
    % previous node.
	for j = (N - 1) : -1 : 1
        for i = 1 : j
            holdValue = (p * optionTree(i, j + 1) + (1 - p) * ...
                optionTree(i+ 1, j + 1)) * exp(-interestRate*(1/N));
            if strcmp('call', type)
                % The value of a call option at experiation is (S - K)^+.
                earlyExercise = max(0, stockTree(i,j) - strikePrice);
            elseif strcmp('put', type)
                % The value of a put option at experiation is (K - S)^+.
                earlyExercise = max(0, strikePrice - stockTree(i,j));
            end
            %Looks at which nodes early exercise is optimal, and uses the
            %obtained maiximum to derive the value of the previous node.
            optionTree(i, j) = max(earlyExercise, holdValue);
            %Looks when early exercise is equal to the holdvalue.
            early(i, j) = abs(holdValue - earlyExercise) < 0.001;
            %Looks when early exercise is really optimal.
            earlybetter(i, j) = (holdValue < earlyExercise);
        end
    end
end

% The delta of a stock option is the ratio of the change in the price of a
% stock option to the change in price of the underlying stock. 
deltahedge = zeros (N, N);
for j= 1 : N - 1
    for i = 1 : j
       deltahedge(i, j) = ...
         (optionTree(i + 1, j + 1) - optionTree(i, j + 1)) / ...
            (stockTree(i + 1, j + 1) - stockTree(i, j + 1));
    end
end    

% vim: expandtab 
